package com.lry.netty01.rx;

import com.lry.netty01.server.utils.ByteUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import lombok.extern.slf4j.Slf4j;

/**
 * 把报文编码成buf发出去，相当于序列化
 */
@Slf4j
public class PacketEncoder extends MessageToByteEncoder<FixedFrame> {

    @Override
    protected void encode(ChannelHandlerContext ctx, FixedFrame fixedFrame, ByteBuf buf) throws Exception {
        buf.writeByte(fixedFrame.getStart());
        buf.writeByte(fixedFrame.getLen());
        buf.writeBytes(fixedFrame.getMsg().getBytes());
        buf.writeByte(fixedFrame.getEnd());

        log.info("电脑串口发送报文："+ByteUtils.preBuf(buf));
    }
}
